package com.yz.common;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.Serializable;

public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer code;
    private String msg;
    private T data;

    public R() {
    }

    public R(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static R error() {
        return error(1, "error");
    }

    public static R error(String msg) {
        return error(1, msg);
    }

    public static R error(int code, String msg) {
        return new R(code, msg);
    }

    public static R ok() {
        return new R(0, "success");
    }

    public static R ok(String msg) {
        return new R(0, msg);
    }

    public static <T> R<T> okData(T data) {
        return new R(0, "success", data);
    }

    public static boolean ifSucc(R r) {
        return 0 == r.getCode();
    }

    public static boolean ifError(R r) {
        return 0 != r.getCode();
    }

    public Integer getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }

    public static <T> T convertValue(Object bean, Class<T> clazz) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.convertValue(bean, clazz);
        } catch (Exception var3) {
            return null;
        }
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public R<T> setData(T data) {
        this.data = data;
        return this;
    }
}
